// 集中管理岗位相关的Hook函数，以实现代码逻辑的复用
import { ref } from "vue";
import { jobGet } from "../api/pro";
import { JobType } from "../types/pro";

export function useJobList() {
  const jobList = ref<Array<JobType>>([]);
  const loading = ref<boolean>(false); //分页加载状态
  const finished = ref<boolean>(false); //是否加载完毕的状态
  let page = 1;
  const handleLoad = () => {
    //首屏及触底后会自动触发
    console.log("load执行了");
    jobGet(page).then((res) => {
      let { results } = res.data;
      if (results.length) {
        jobList.value = [
          ...jobList.value, //原数据
          ...results, //新数据
        ];
        loading.value = false; //每次请求完毕后，需要重置loading，保证下次请求正常发起
        page++;
        return;
      }
      finished.value = true; //结束分页请求交互，触底将不再发起请求
    });
  };
  return {
    jobList,
    loading,
    finished,
    handleLoad,
  };
}
